package ihm.views.preferences;

import ihm.actions.CloseFrameAction;
import ihm.actions.DataBaseSaveAction;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import systems.configurations.SystemConfiguration;
import systems.languages.MultiLanguages;

public class DataBaseBox extends JDialog
{

  private static final long serialVersionUID = 1L;

  private String titleLabel;
  //Driver
  private String driverLabel;
  private JTextField driverTextField;
  //Url
  private String urlLabel;
  private JTextField urlTextField;
  //Table
  private String dbLabel;
  private JTextField dbTextField;
  //User and Pwd
  private String userLabel;
  private JTextField userTextField;
  private String pwdLabel;
  private JTextField pwdTextField;
  //Buttons
  private JButton Save= new JButton();
  private JButton Cancel = new JButton();
  //Panel stuff
  private LayoutManager gridBagLayout = new GridBagLayout();
  private GridBagConstraints  gbc = new GridBagConstraints();


  public DataBaseBox()
  {
    super();
    initStrings();
    setupDialog();
    buildDialog();
    buildUsrAndPwd();
    buildButtons();
    setVisible(true);
  }

  private void initStrings()
  {
    titleLabel = MultiLanguages.getProperties("dialog.DBconfig.title");
    driverLabel = MultiLanguages.getProperties("dialog.DBconfig.driver");
    urlLabel = MultiLanguages.getProperties("dialog.DBconfig.url");
    dbLabel = MultiLanguages.getProperties("dialog.DBconfig.table");
    userLabel = MultiLanguages.getProperties("dialog.DBconfig.user");
    pwdLabel = MultiLanguages.getProperties("dialog.DBconfig.pwd");
    Save.setText(
        MultiLanguages.getProperties("dialog.DBconfig.Save")
    );
    Cancel.setText(
        MultiLanguages.getProperties("dialog.DBconfig.Cancel")
    );
  }

  private void setupDialog()
  {
    this.setLayout(gridBagLayout);
    this.setTitle(titleLabel);
    this.setModal(true);
    this.setSize(400, 340);
 //   this.setResizable(false);
  }

  private void buildDialog()
  {
    buildDriver();
    buildUrl();
    buildDataBase();
  }

  private void buildDriver() {
    int gridx = 0;
    int gridy = 0;
    //--Label
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    this.add(new JLabel(parseString(driverLabel)), gbc);

    //TextField
    gridy++;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.driverTextField = new JTextField(SystemConfiguration.getProperties().getProperty("db.driver"));
    this.add(this.driverTextField,gbc);
  }

  private void buildUrl() {
    int gridx = 0;
    int gridy = 2;
    //--Label
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    this.add(new JLabel(parseString(urlLabel)), gbc);

    //TextField
    gridy++;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    this.urlTextField = new JTextField();
    this.add(this.urlTextField,gbc);    
  }

  private void buildDataBase() {
    int gridx = 0;
    int gridy = 4;
    //--Label
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    this.add(new JLabel(parseString(dbLabel)), gbc);

    //TextField
    gridy++;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    this.dbTextField = new JTextField();
    this.add(this.dbTextField,gbc);    
  }

  private void buildUsrAndPwd()
  {
    int gridx = 0;
    int gridy = 6;
    //--Labels
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    this.add(new JLabel(parseString(userLabel)), gbc);
    gridx ++;
    gbc.gridx = gridx;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    this.add(new JLabel(parseString(pwdLabel)),gbc);
    
    //TextFields
    gridx--;
    gridy++;
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    this.userTextField = new JTextField(
        SystemConfiguration.getProperties().getProperty("db.remote.user")
    );
    this.userTextField.setColumns(15);
    this.add(this.userTextField,gbc); 
    gridx ++;
    gbc.gridx = gridx;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    this.pwdTextField = new JTextField(
        SystemConfiguration.getProperties().getProperty("db.remote.pass")
    );
    this.add(this.pwdTextField,gbc);   
  }
  
  
  private void buildButtons()
  {
    int gridx = 1;
    int gridy = 8;
    
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.fill = GridBagConstraints.NONE;
    this.Save = new JButton(MultiLanguages.getProperties("dialog.DBconfig.Save"));
    this.Save.addActionListener(
        new DataBaseSaveAction(this)
    );
    this.add(this.Save);
    
    
    gridx ++;
    gbc.gridx = gridx;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    this.Cancel = new JButton(MultiLanguages.getProperties("dialog.DBconfig.Cancel"));
    this.Cancel.addActionListener(new CloseFrameAction(this));
    this.add(this.Cancel);
  }
  private String parseString(String stringToParse)
  {
    String parsedString = stringToParse;
    parsedString = parsedString.replaceAll("\\$n", "<br/>");
    parsedString = parsedString.replaceAll("\\$i", "<i>");
    parsedString = parsedString.replaceAll("\\$_i", "</i>");
    parsedString = "<html>"+parsedString+"</html>";
    return parsedString;
  }

  /**
   * @return the driverTextField
   */
  public JTextField getDriverTextField() {
    return driverTextField;
  }

  /**
   * @return the urlTextField
   */
  public JTextField getUrlTextField() {
    return urlTextField;
  }

  /**
   * @return the dbTextField
   */
  public JTextField getDbTextField() {
    return dbTextField;
  }

  /**
   * @return the userTextField
   */
  public JTextField getUserTextField() {
    return userTextField;
  }

  /**
   * @return the pwdTextField
   */
  public JTextField getPwdTextField() {
    return pwdTextField;
  }
  
  public List<String> getNewConfig()
  {
    LinkedList<String> newConfig = new LinkedList<String>();
    //Driver
    newConfig.add(driverTextField.getText().trim());
    //Url
    newConfig.add(urlTextField.getText().trim());
    //DataBase name
    newConfig.add(dbTextField.getText().trim());
    //User
    newConfig.add(userTextField.getText().trim());
    //Pwd
    newConfig.add(pwdTextField.getText().trim());
    return newConfig;
  }
  
}
